#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned ll
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define pii pair<int,int>
#define x first
#define y second
#define pb push_back
#define all(v) v.begin(),v.end()
#define lbt(i) (i&-i)
using namespace std;
const int N = 131075*2, mod = 998244353;
int a1[N],c[N],n,m,a[N],mx;
bool d[N][20];
int res[N][20];
int callog(int a) {
    int t=0,s=1;
    while(true) {
        if(s>=a) return t;
        ++t,s*=2;
    }
}
int cpow(int a) {
    int t=0,s=1;
    while(true) {
        if(s>=a) return s;
        ++t,s*=2;
    }
}
bool ok[N];
pii qj[N];
void cal(int l,int r,int lim,int cs,int ch) {
    if(l==r) {
        ok[l]=1;
        if(l<=lim) qj[l]={a[l],a[l]};
        else qj[l]={0,2147483647};
        return;
    } int mid=l+r>>1;
    cal(l,mid,lim,cs-1,ch*2-1),cal(mid+1,r,lim,cs-1,ch*2);
    if(d[cs][ch]==0) {
        bool flag=0;
        rep(i,l,mid) {
            // cout<<"dbg: "<<i<<' '<<qj[i].x<<' '<<qj[i].y<<'\n';
            if(qj[i].x>qj[i].y) ok[i]=0;
            if(qj[i].y<cs&&ok[i]) flag=1,ok[i]=0;
            if(qj[i].x<cs&&ok[i]) flag=1;
            qj[i].x=max(qj[i].x,cs);
        }
        if(!flag) rep(i,mid+1,r) ok[i]=0;
    } else {
        bool flag=0;
        rep(i,mid+1,r) {
            // cout<<"dbg: "<<i<<' '<<qj[i].x<<' '<<qj[i].y<<'\n';
            if(qj[i].x>qj[i].y) ok[i]=0;
            if(qj[i].y<cs&&ok[i]) flag=1,ok[i]=0;
            if(qj[i].x<cs&&ok[i]) flag=1;
            qj[i].x=max(qj[i].x,cs);
        }
        if(!flag) rep(i,l,mid) ok[i]=0;
    }
    // cerr<<l<<' '<<r<<' '<<cs<<' '<<ch<<' '<<d[cs][ch]<<":\n";
    // rep(i,l,r) if(ok[i]) cout<<i<<' '; cout<<'\n';     
}
void solve() {
    ll res=0;
    // cerr<<"a[]: "; rep(i,1,n) cerr<<a[i]<<" \n"[i==n];
    rep(i,1,m) {
        int pw=cpow(c[i]),cs=callog(c[i]);
        ll ret=0;
        cal(1,pw,c[i],cs,1);
        // cerr<<"dbg("<<c[i]<<"): ";
        rep(j,1,pw) if(ok[j]) ret+=j;// cout<<j<<' ';
        // cout<<'\n';
        // cerr<<"dbg("<<c[i]<<"): "<<ret<<' '<<ret*i<<'\n';
        res^=ret*i;
    } cout<<res<<'\n';
}
signed main() {
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    rep(i,1,n) cin>>a1[i];
    rep(i,1,m) cin>>c[i],mx=max(mx,c[i]);
    for(int tmp=cpow(n)/2,i=1;tmp;tmp/=2,++i) rep(j,1,tmp) {
        char c; cin>>c;
        if(c=='0') d[i][j]=0;
        else if(c=='1') d[i][j]=1;
        else --j;
    } int T; cin>>T;
    while(T--) {
        int x[4];
        rep(i,0,3) cin>>x[i];
        rep(i,1,n) a[i]=a1[i]^x[i%4];
        solve();
    }
    return 0;
}
/*
!!! read the problem carefully at least twice !!!
!! keep a positive attitude, confident and calm, ur attitude counts!
1. think carefully before u start
2. when debugging, don't output much, firstly read the whole program again
3. balance the loss and get, this is important
4. do all the easy bruteforce first
5. remember to duipai, check the correctness and time.
6. long long, devide zero, array out of range!

at last:
checker the version of the program and put it under the correct folder
check extra output or wrong output of samples 
*/